package com.template.project.domain;

import com.template.project.code.ErrorCode;
import com.template.project.code.UserErrorCode;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.C;

/**
 * 请求返回对象
 *
 * @author xs
 */
@Data
public class ResponseResult<T> {

    public static final int OK_CODE = 0;

    public static final String OK_MSG = "success";

    private Integer code;

    private String level;

    private String msg;

    private Boolean ok;

    private T data;

    public ResponseResult(Integer code, String level, boolean ok, String msg, T data) {
        this.code = code;
        this.level = level;
        this.ok = ok;
        this.msg = msg;
        this.data = data;
    }

    public ResponseResult(ErrorCode errorCode, boolean ok, String msg, T data) {
        this.code = errorCode.getCode();
        this.level = errorCode.getLevel();
        this.ok = ok;
        if (StringUtils.isNotBlank(msg)) {
            this.msg = msg;
        } else {
            this.msg = errorCode.getMsg();
        }
        this.data = data;
    }

    public static <T> ResponseResult<T> ok() {
        return new ResponseResult<>(OK_CODE, null, true, OK_MSG, null);
    }

    public static <T> ResponseResult<T> ok(T data) {
        return new ResponseResult<>(OK_CODE, null, true, OK_MSG, data);
    }

    public static <T> ResponseResult<T> okMsg(String msg) {
        return new ResponseResult<>(OK_CODE, null, true, msg, null);
    }

    // -------------------------------------------- 最常用的 用户参数 错误码 --------------------------------------------

    public static <T> ResponseResult<T> userErrorParam() {
        return new ResponseResult<>(UserErrorCode.PARAM_ERROR, false, null, null);
    }

    public static <T> ResponseResult<T> userErrorParam(String msg) {
        return new ResponseResult<>(UserErrorCode.PARAM_ERROR, false, msg, null);
    }

    // -------------------------------------------- 错误码 --------------------------------------------

    public static <T> ResponseResult<T> error(ErrorCode errorCode) {
        return new ResponseResult<>(errorCode, false, null, null);
    }

    public static <T> ResponseResult<T> error(ErrorCode errorCode, boolean ok) {
        return new ResponseResult<>(errorCode, ok, null, null);
    }

    public static ResponseResult<C> error(ResponseResult<C> responseResult) {
        return new ResponseResult<>(responseResult.getCode(), responseResult.getLevel(), responseResult.getOk(),
            responseResult.getMsg(), responseResult.getData());
    }

    public static <T> ResponseResult<T> error(ErrorCode errorCode, String msg) {
        return new ResponseResult<>(errorCode, false, msg, null);
    }

    public static <T> ResponseResult<T> errorData(ErrorCode errorCode, T data) {
        return new ResponseResult<>(errorCode, false, null, data);
    }

}
